var x = {};

x.init = function(appInstance){
    this.app = appInstance;
    return this;
};

x.setApiName = function(name){
    this.apiName = name;
    return this;
};

x.data = {};

x.onLoad = function(option)
{
    this.setData({option:option});
    this.setPage(1);
    wx.startPullDownRefresh();
};

x.setPage = function(page)
{
    this.setData({page: page});
};

x.loadData = function()
{
    function success(response)
    {
        var dataTemp = response.data.data;
        var list = [];
        if(that.data.page > 1){
            list = that.data.list.concat(dataTemp.list);
        }
        else{
            list = dataTemp.list;
        }
        var data = {
            list: list,
            next: dataTemp.next,
            count: dataTemp.count
        };
        if(dataTemp.oss_domain){
            data.ossDomain = dataTemp.oss_domain;
        }
        that.setData(data);
        wx.stopPullDownRefresh();
    }
    
    var that = this;
    var data = {
        token: this.app.dry.getToken(),
        device: this.app.config.device,
        page: this.data.page
    };
    for(let key in that.data.option){
        data[key] = that.data.option[key];
    }
    this.app.r().setData(data).setSuccess(this.app.msg.ifFailStop).setSuccess(success).setLoadingText("正在加载数据").run(this.apiName);
};

x.onPullDownRefresh = function()
{
    this.setPage(1);
    this.loadData();
};

x.onReachBottom = function()
{
    if(this.data.next){
        this.setPage(this.data.page + 1);
        this.loadData();
    }
};

module.exports = x;